package com.hotai.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotai.comment.dtos.PageResponseResult;
import com.hotai.comment.dtos.ResponseResult;
import com.hotai.comment.enums.AppHttpCodeEnum;
import com.hotai.comment.pojo.*;
import com.hotai.mapper.*;
import com.hotai.service.AccountService;
import com.hotai.service.TongService;
import com.hotai.vo.account.AccountSuject;
import com.hotai.vo.account.AccountWee;
import com.hotai.vo.course.CourseDetailList;
import com.hotai.vo.she.SheBao;
import com.hotai.vo.she.SheBaoList;
import com.hotai.vo.tongji.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class TongServiceImpl implements TongService {

    @Autowired
    private CoursewareStudentMapper coursewareStudentMapper;


    @Autowired
    private UserTeacherMapper userTeacherMapper;

    @Autowired
    private UserStudentMapper userStudentMapper;

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private CoursewareBaoStudentMapper coursewareBaoStudentMapper;

    @Autowired
    private AccountServiceImpl accountService;

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public ResponseResult XueTongSt(XuekernelPage xuekernelPage) {
//        if (xuekernelPage == null) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, AppHttpCodeEnum.PARAM_INVALID.getErrorMessage());
//        }

        Xuekernel xueTong = new Xuekernel();
        //总是长
        if (xuekernelPage.getStatus() == null) {
            xuekernelPage.setStatus(0);
        }
        if (xuekernelPage.getStatus() == 1 || xuekernelPage.getStatus() == 0) {

            Integer time = coursewareStudentMapper.xueshi(xuekernelPage);
            if (time == null) {
                time = 0;
            }
            //新增数量
            Integer userCount = userStudentMapper.zeng(xuekernelPage);
            //用户总数量
            Integer userzCount = userStudentMapper.UserCount(xuekernelPage);

            //设备总量
            Integer eqCount = equipmentMapper.tongEq(xuekernelPage);
            //学习次数
            Integer xuecount = coursewareStudentMapper.xueCount(xuekernelPage);
            //平均完课率
            //这一天完成
            Integer accomplish = coursewareStudentMapper.accomplishCount(xuekernelPage);

            Integer courCount = coursewareStudentMapper.xueCount(xuekernelPage);
            if (xuekernelPage.getTime() == null) {
                xuekernelPage.setTime(new Date());
            }
            Date yesterday = minDate(xuekernelPage.getTime(), 1);
            xuekernelPage.setTime(yesterday);
            Integer timeyester = coursewareStudentMapper.xueshi(xuekernelPage);
            if (timeyester == null) {
                timeyester = 0;
            }
            //新增数量
            Integer userCountyester = userStudentMapper.zeng(xuekernelPage);
            //用户总数量
            Integer userzCountyester = userStudentMapper.UserCount(xuekernelPage);

            //设备总量
            Integer eqCountyester = equipmentMapper.tongEq(xuekernelPage);
            //学习次数
            Integer xuecountyester = coursewareStudentMapper.xueCount(xuekernelPage);
            //平均完课率
            //这一天完成
            Integer accomplishyester = coursewareStudentMapper.accomplishCount(xuekernelPage);

            Integer courCountyester = coursewareStudentMapper.xueCount(xuekernelPage);
            xueTong.setZong(time);
            xueTong.setAddCount(userCount);
            xueTong.setEqCount(eqCount);
            xueTong.setUserCount(userzCount);
            xueTong.setXcount(xuecount);
            if (accomplish > 0) {
                xueTong.setAvgCount(accomplish * 100 / courCount);
            }
            if (accomplish == 0) {
                xueTong.setAvgCount(0);
            }
            //比较上一次
            if (timeyester == 0) {
                xueTong.setZongLast(0);
            } else {
                xueTong.setZongLast((time - timeyester) * 100 / timeyester);
            }
            if (userCountyester == 0) {
                xueTong.setAddCountLast(0);
            } else {
                xueTong.setAddCountLast((userCount - userCountyester) * 100 / userCountyester);
            }
            if (userzCountyester == 0) {
                xueTong.setUserCountLast(0);
            } else {
                xueTong.setUserCountLast((userzCount - userzCountyester) * 100 / userzCountyester);
            }
            if (eqCountyester == 0) {
                xueTong.setEqCountLast(0);
            } else {
                xueTong.setEqCountLast((eqCount - eqCountyester) * 100 / eqCountyester);
            }
            if (xuecountyester == 0) {
                xueTong.setXcountLast(0);
            } else {
                xueTong.setXcountLast((xuecount - xuecountyester) * 100 / xuecountyester);
            }
            if (accomplishyester > 0) {
                //上一次平均完课率
                int i = accomplishyester * 100 / courCountyester;
                xueTong.setAvgCountLast((xueTong.getAvgCount()-i)*100/i);
            }
            if (accomplishyester == 0) {
                xueTong.setAvgCountLast(0);
            }
            xueTong.setStatus(1);

        } else if (xuekernelPage.getStatus() == 2) {
            Integer time = coursewareStudentMapper.xueshi(xuekernelPage);
            if (time == null) {
                time = 0;
            }
            //新增数量
            Integer userCount = userStudentMapper.zeng(xuekernelPage);
            //用户总数量
            Integer userzCount = userStudentMapper.UserCount(xuekernelPage);

            //设备总量
            Integer eqCount = equipmentMapper.tongEq(xuekernelPage);
            //学习次数
            Integer xuecount = coursewareStudentMapper.xueCount(xuekernelPage);
            //平均完课率
            //这一天完成
            Integer accomplish = coursewareStudentMapper.accomplishCount(xuekernelPage);

            Integer courCount = coursewareStudentMapper.xueCount(xuekernelPage);
            //相差天数
            int i = differentDays(xuekernelPage.getMinTime(), xuekernelPage.getMaxTime());
            //上一周时间
            Date mintime = minDate(xuekernelPage.getMinTime(), i);

            Date maxtime = minDate(xuekernelPage.getMaxTime(), i);
            xuekernelPage.setMinTime(mintime);
            xuekernelPage.setMaxTime(maxtime);

            Integer timeyester = coursewareStudentMapper.xueshi(xuekernelPage);
            if (timeyester == null) {
                timeyester = 0;
            }
            //新增数量
            Integer userCountyester = userStudentMapper.zeng(xuekernelPage);
            //用户总数量
            Integer userzCountyester = userStudentMapper.UserCount(xuekernelPage);

            //设备总量
            Integer eqCountyester = equipmentMapper.tongEq(xuekernelPage);
            //学习次数
            Integer xuecountyester = coursewareStudentMapper.xueCount(xuekernelPage);
            //平均完课率
            //这一天完成
            Integer accomplishyester = coursewareStudentMapper.accomplishCount(xuekernelPage);
            Integer courCountyester = coursewareStudentMapper.xueCount(xuekernelPage);
            xueTong.setZong(time);
            xueTong.setAddCount(userCount);
            xueTong.setEqCount(eqCount);
            xueTong.setUserCount(userzCount);
            xueTong.setXcount(xuecount);
            //比较上一次
//            xueTong.setAddCountLast(userCountyester);
//            xueTong.setUserCountLast(userzCountyester);
//            xueTong.setEqCountLast(eqCountyester);
//            xueTong.setZongLast(timeyester);
//            xueTong.setXcountLast(xuecountyester);
            if (accomplish > 0) {
                xueTong.setAvgCount(accomplish * 100 / courCount);
            }
            if (accomplish == 0) {
                xueTong.setAvgCount(0);
            }
            //比较上一次
            if (timeyester == 0) {
                xueTong.setZongLast(0);
            } else {
                xueTong.setZongLast((time - timeyester) * 100 / timeyester);
            }
            if (userCountyester == 0) {
                xueTong.setAddCountLast(0);
            } else {
                xueTong.setAddCountLast((userCount - userCountyester) * 100 / userCountyester);
            }
            if (userzCountyester == 0) {
                xueTong.setUserCountLast(0);
            } else {
                xueTong.setUserCountLast((userzCount - userzCountyester) * 100 / userzCountyester);
            }
            if (eqCountyester == 0) {
                xueTong.setEqCountLast(0);
            } else {
                xueTong.setEqCountLast((eqCount - eqCountyester) * 100 / eqCountyester);
            }
            if (xuecountyester == 0) {
                xueTong.setXcountLast(0);
            } else {
                xueTong.setXcountLast((xuecount - xuecountyester) * 100 / xuecountyester);
            }
            if (accomplishyester > 0) {
                //上一次平均完课率
                int Avgi = accomplishyester * 100 / courCountyester;
                xueTong.setAvgCountLast((xueTong.getAvgCount()-Avgi)*100/Avgi);
            }
            if (accomplishyester == 0) {
                xueTong.setAvgCountLast(0);
            }
            xueTong.setStatus(2);

        } else if (xuekernelPage.getStatus() == 3) {
            Integer time = coursewareStudentMapper.xueshi(xuekernelPage);
            if (time == null) {
                time = 0;
            }
            //新增数量
            Integer userCount = userStudentMapper.zeng(xuekernelPage);
            //用户总数量
            Integer userzCount = userStudentMapper.UserCount(xuekernelPage);

            //设备总量
            Integer eqCount = equipmentMapper.tongEq(xuekernelPage);
            //学习次数
            Integer xuecount = coursewareStudentMapper.xueCount(xuekernelPage);
            //平均完课率
            //这一天完成
            Integer accomplish = coursewareStudentMapper.accomplishCount(xuekernelPage);

            Integer courCount = coursewareStudentMapper.xueCount(xuekernelPage);
            //相差天数
            //上一周时间

            Date date = monthDate(xuekernelPage.getTime(), 1);
            xuekernelPage.setTime(date);
            Integer timeyester = coursewareStudentMapper.xueshi(xuekernelPage);
            if (timeyester == null) {
                timeyester = 0;
            }
            //新增数量
            Integer userCountyester = userStudentMapper.zeng(xuekernelPage);
            //用户总数量
            Integer userzCountyester = userStudentMapper.UserCount(xuekernelPage);
            //设备总量
            Integer eqCountyester = equipmentMapper.tongEq(xuekernelPage);
            //学习次数
            Integer xuecountyester = coursewareStudentMapper.xueCount(xuekernelPage);
            //平均完课率
            //这一天完成
            Integer accomplishyester = coursewareStudentMapper.accomplishCount(xuekernelPage);
            Integer courCountyester = coursewareStudentMapper.xueCount(xuekernelPage);
            xueTong.setZong(time);
            xueTong.setAddCount(userCount);
            xueTong.setEqCount(eqCount);
            xueTong.setUserCount(userzCount);
            xueTong.setXcount(xuecount);
            if (accomplish > 0) {
                xueTong.setAvgCount(accomplish * 100 / courCount);
            }
            if (accomplish == 0) {
                xueTong.setAvgCount(0);
            }
            //比较上一次
//            xueTong.setAddCountLast(userCountyester);
//            xueTong.setUserCountLast(userzCountyester);
//            xueTong.setEqCountLast(eqCountyester);
//            xueTong.setZongLast(timeyester);
//            xueTong.setXcountLast(xuecountyester);
            if (accomplish > 0) {
                xueTong.setAvgCount(accomplish * 100 / courCount);
            }
            if (accomplish == 0) {
                xueTong.setAvgCount(0);
            }
            //比较上一次
            if (timeyester == 0) {
                xueTong.setZongLast(0);
            } else {
                xueTong.setZongLast((time - timeyester) * 100 / timeyester);
            }
            if (userCountyester == 0) {
                xueTong.setAddCountLast(0);
            } else {
                xueTong.setAddCountLast((userCount - userCountyester) * 100 / userCountyester);
            }
            if (userzCountyester == 0) {
                xueTong.setUserCountLast(0);
            } else {
                xueTong.setUserCountLast((userzCount - userzCountyester) * 100 / userzCountyester);
            }
            if (eqCountyester == 0) {
                xueTong.setEqCountLast(0);
            } else {
                xueTong.setEqCountLast((eqCount - eqCountyester) * 100 / eqCountyester);
            }
            if (xuecountyester == 0) {
                xueTong.setXcountLast(0);
            } else {
                xueTong.setXcountLast((xuecount - xuecountyester) * 100 / xuecountyester);
            }
            if (accomplishyester > 0) {
                int Avgi = accomplishyester * 100 / courCountyester;
                xueTong.setAvgCountLast((xueTong.getAvgCount()-Avgi)*100/Avgi);
            }
            if (accomplishyester == 0) {
                xueTong.setAvgCountLast(0);
            }
            xueTong.setStatus(3);
        } else if (xuekernelPage.getStatus() == 4) {
            //总是长
            Integer time = coursewareStudentMapper.xueshi(xuekernelPage);
            if (time == null) {
                time = 0;
            }
            //新增数量
            Integer userCount = userStudentMapper.zeng(xuekernelPage);
            //用户总数量
            Integer userzCount = userStudentMapper.UserCount(xuekernelPage);

            //设备总量
            Integer eqCount = equipmentMapper.tongEq(xuekernelPage);
            //学习次数
            Integer xuecount = coursewareStudentMapper.xueCount(xuekernelPage);
            //平均完课率
            //这一天完成
            Integer accomplish = coursewareStudentMapper.accomplishCount(xuekernelPage);

            Integer courCount = coursewareStudentMapper.xueCount(xuekernelPage);
            Date yesterday = YesDate(xuekernelPage.getTime(), 1);
            xuekernelPage.setTime(yesterday);
            Integer timeyester = coursewareStudentMapper.xueshi(xuekernelPage);
            if (timeyester == null) {
                timeyester = 0;
            }
            //新增数量
            Integer userCountyester = userStudentMapper.zeng(xuekernelPage);
            //用户总数量
            Integer userzCountyester = userStudentMapper.UserCount(xuekernelPage);

            //设备总量
            Integer eqCountyester = equipmentMapper.tongEq(xuekernelPage);
            //学习次数
            Integer xuecountyester = coursewareStudentMapper.xueCount(xuekernelPage);
            //平均完课率
            //这一天完成
            Integer accomplishyester = coursewareStudentMapper.accomplishCount(xuekernelPage);

            Integer courCountyester = coursewareStudentMapper.xueCount(xuekernelPage);
            xueTong.setZong(time);
            xueTong.setAddCount(userCount);
            xueTong.setEqCount(eqCount);
            xueTong.setUserCount(userzCount);
            xueTong.setXcount(xuecount);
            if (accomplish > 0) {
                xueTong.setAvgCount(accomplish * 100 / courCount);
            }
            if (accomplish == 0) {
                xueTong.setAvgCount(0);
            }
            //比较上一次
            if (timeyester == 0) {
                xueTong.setZongLast(0);
            } else {
                xueTong.setZongLast((time - timeyester) * 100 / timeyester);
            }
            if (userCountyester == 0) {
                xueTong.setAddCountLast(0);
            } else {
                xueTong.setAddCountLast((userCount - userCountyester) * 100 / userCountyester);
            }
            if (userzCountyester == 0) {
                xueTong.setUserCountLast(0);
            } else {
                xueTong.setUserCountLast((userzCount - userzCountyester) * 100 / userzCountyester);
            }
            if (eqCountyester == 0) {
                xueTong.setEqCountLast(0);
            } else {
                xueTong.setEqCountLast((eqCount - eqCountyester) * 100 / eqCountyester);
            }
            if (xuecountyester == 0) {
                xueTong.setXcountLast(0);
            } else {
                xueTong.setXcountLast((xuecount - xuecountyester) * 100 / xuecountyester);
            }
            if (accomplishyester > 0) {
                int Avgi = accomplishyester * 100 / courCountyester;
                xueTong.setAvgCountLast((xueTong.getAvgCount()-Avgi)*100/Avgi);
            }
            if (accomplishyester == 0) {
                xueTong.setAvgCountLast(0);
            }
            xueTong.setStatus(4);
        }

        return ResponseResult.okResult(xueTong);
    }

    public Date monthDate(Date time, Integer i) {
        //获取当前日期
        Calendar theCa = Calendar.getInstance();
        theCa.setTime(time);
        theCa.add(theCa.MONTH, -i); //天份
        Date start = theCa.getTime();
        return start;
    }

    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }

    //去年
    public Date YesDate(Date time, Integer i) {
        //获取当前日期
        Calendar theCa = Calendar.getInstance();
        theCa.setTime(time);
        theCa.add(theCa.YEAR, -i); //天份
        Date start = theCa.getTime();
        return start;
    }

    //昨天
    public Date minDate(Date time, Integer i) {
        //获取当前日期
        Calendar theCa = Calendar.getInstance();
        theCa.setTime(time);
        theCa.add(theCa.DATE, -i); //天份
        Date start = theCa.getTime();
        return start;
    }

    //学习数据概况
    public List<String> day(Date stDate, int j) {
        ArrayList<String> arrayList = new ArrayList<>();
        //获取当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");

        String endDate = sdf.format(stDate); //当前日期
        //获取三十天前日期
        for (int i = j; i >= 0; i--) {
            Calendar theCa = Calendar.getInstance();
            theCa.setTime(stDate);
            theCa.add(theCa.DATE, -i); //最后一个数字30可改，30天的意思
            Date start = theCa.getTime();
            String startDate = sdf.format(start); //三十天之前日期
            arrayList.add(startDate);
        }
        return arrayList;
    }

    public List<String> dayList1( Date today, int j) {
        ArrayList<String> arrayList = new ArrayList<>();
        //获取当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        String endDate = sdf.format(today); //当前日期
        //获取三十天前日期
        for (int i = j; i >= 0; i--) {
            Calendar theCa = Calendar.getInstance();
            theCa.setTime(today);
            theCa.add(theCa.DATE, -i); //最后一个数字30可改，30天的意思
            Date start = theCa.getTime();
            String startDate = sdf.format(start); //三十天之前日期
            arrayList.add(startDate);
        }
        return arrayList;
    }

    public List<String> dayList() {
        ArrayList<String> arrayList = new ArrayList<>();
        //获取当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        Date today = new Date();
        String endDate = sdf.format(today); //当前日期
        //获取三十天前日期
        for (int i = 30; i >= 0; i--) {
            Calendar theCa = Calendar.getInstance();
            theCa.setTime(today);
            theCa.add(theCa.DATE, -i); //最后一个数字30可改，30天的意思
            Date start = theCa.getTime();
            String startDate = sdf.format(start); //三十天之前日期
            arrayList.add(startDate);
        }
        return arrayList;
    }


    //学习数据概况
    public List<String> dayxue(Date stDate, int j) {
        ArrayList<String> arrayList = new ArrayList<>();
        //获取当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");

        String endDate = sdf.format(stDate); //当前日期
        //获取三十天前日期
        for (int i = 0; i < j; i++) {
            Calendar theCa = Calendar.getInstance();
            theCa.setTime(stDate);
            theCa.add(theCa.DATE, +i); //最后一个数字30可改，30天的意思
            Date start = theCa.getTime();
            String startDate = sdf.format(start); //三十天之前日期
            arrayList.add(startDate);
        }
        return arrayList;
    }

    @Override
    public ResponseResult TearTong() {

        //从数据库中查询指定天数之前到当前每天的订单数
        List<Map<String, Object>> orders = coursewareStudentMapper.tongTear();
//补全数据库中不存在的日期，订单数为0

        List<String> dayList = dayList();
        boolean exists = false;
        for (int i = 0; i < dayList.size(); i++) {
            exists = false;
            for (Map<String, Object> hs : orders) {
                if (dayList.get(i).equals(hs.get("shoDate"))) {
                    exists = true;
                    break;
                }
            }
            if (!exists) {
                final String date2 = dayList.get(i);
                orders.add(i, new HashMap<String, Object>() {{
                    put("count", 0);
                    put("shoDate", date2);
                }});
            }
        }
        return ResponseResult.okResult(orders);
    }


    @Override
    public ResponseResult Teart() {
        TeaShu teaShu = new TeaShu();
        List<UserTeacher> userTeachers = userTeacherMapper.selectList(Wrappers.<UserTeacher>lambdaQuery().eq(UserTeacher::getDeid, 1));

        List<UserStudent> userStudents = userStudentMapper.selectList(Wrappers.<UserStudent>lambdaQuery().eq(UserStudent::getDeid, 1));

        List<Equipment> equipment = equipmentMapper.selectList(Wrappers.<Equipment>lambdaQuery().eq(Equipment::getDeid, 1));
        List<CoursewareStudent> coursewareStudents = coursewareStudentMapper.selectList(null);

        TeaShu teaShu1 = coursewareStudentMapper.shipin();
        if (teaShu1 != null) {
            teaShu.setTcount(userTeachers.size());
            teaShu.setXcount(userStudents.size());
            teaShu.setEcount(equipment.size());
            teaShu.setIcount(coursewareStudents.size());
            if (teaShu1.getDu() == null) {
                teaShu1.setDu(0);
            }
            teaShu.setDu(teaShu1.getDu());
        }

        return ResponseResult.okResult(teaShu);
    }

    @Override
    public ResponseResult ShoT() {
        ArrayList<TSho> tSho = coursewareStudentMapper.ShoT();
        return ResponseResult.okResult(tSho);
    }

    @Override
    public ResponseResult ShoCour() {
        ArrayList<TSho> tShos = coursewareStudentMapper.ShoCour();
        return ResponseResult.okResult(tShos);
    }

    @Override
    public ResponseResult CourseTong(CoursewareTong queryware) {
        if (queryware == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, AppHttpCodeEnum.PARAM_INVALID.toString());
        }
        queryware.checkParam();
//        queryware.setPage((queryware.getPage()-1)*queryware.getSize());
        PageHelper.startPage(queryware.getPage(), queryware.getSize());

        Map<String, Object> paramMap = new HashMap<String, Object>();
        String searchParity = queryware.getClas();
        if (StringUtils.isNotBlank(searchParity)) {
            List<String> searchParityList = Arrays.asList(searchParity.split(","));
            paramMap.put("clas", searchParityList);
        }
        queryware.setParamMap(paramMap);

        ArrayList<CoursewaretList> detaile = coursewareStudentMapper.courTong(queryware);
        PageInfo<CoursewaretList> pageInfo = new PageInfo<>(detaile);
        PageResponseResult responseResult = new PageResponseResult(queryware.getPage(), pageInfo.getSize(), (int) pageInfo.getTotal());
        responseResult.setData(detaile);
        return responseResult;
    }


    @Override
    public ResponseResult CourwareTong(XuekernelPage xuekernelPage) {
        if (xuekernelPage.getStatus()==null){
            xuekernelPage.setStatus(0);
        }
        ArrayList<SubjectTong> userStudentXueCount = null;
        if (xuekernelPage.getStatus() == 1 || xuekernelPage.getStatus() == 0) {
            userStudentXueCount = coursewareStudentMapper.tongXueShu(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 2) {
            userStudentXueCount = coursewareStudentMapper.tongXueShu(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 3) {
            userStudentXueCount = coursewareStudentMapper.tongXueShu(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 4) {
            userStudentXueCount = coursewareStudentMapper.tongXueShu(xuekernelPage);
        }
        return kong(userStudentXueCount);
    }


    //分类
    public ResponseResult kong(ArrayList<SubjectTong> subjectTongs) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("理化生实验");
        strings.add("数学几何");
        strings.add("心理健康");
        strings.add("校园安全");
        strings.add("人文地理");
        boolean exit = false;
        for (int i = 0; i < strings.size(); i++) {
            exit = false;
            for (SubjectTong suject : subjectTongs) {
                if (strings.get(i).equals(suject.getFname())) {
                    exit = true;
                    break;
                }
            }
            if (!exit) {
                SubjectTong accountSuject = new SubjectTong();
                accountSuject.setFname(strings.get(i));
                accountSuject.setCount(0);
                subjectTongs.add(accountSuject);
            }
        }
        Collections.sort(subjectTongs, new Comparator<SubjectTong>() {
            @Override
            public int compare(SubjectTong o1, SubjectTong o2) {
                return o2.getFname().compareTo(o1.getFname());
            }
        });
        return ResponseResult.okResult(subjectTongs);
    }


    @Override
    public ResponseResult CourwareTongShi(XuekernelPage xuekernelPage) {
        if (xuekernelPage.getStatus()==null){
            xuekernelPage.setStatus(0);
        }
        ArrayList<SubjectTong> userStudentXueCount = null;
        if (xuekernelPage.getStatus() == 1 || xuekernelPage.getStatus() == 0) {
            userStudentXueCount = coursewareStudentMapper.tongWareShi(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 2) {
            userStudentXueCount = coursewareStudentMapper.tongWareShi(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 3) {
            userStudentXueCount = coursewareStudentMapper.tongWareShi(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 4) {
            userStudentXueCount = coursewareStudentMapper.tongWareShi(xuekernelPage);
        }
        return kong(userStudentXueCount);

    }

    @Override
    public ResponseResult CourwareTongPin(XuekernelPage xuekernelPage) {
        if (xuekernelPage.getStatus()==null){
            xuekernelPage.setStatus(0);
        }
        ArrayList<SubjectTong> userStudentXueCount = null;
        if (xuekernelPage.getStatus() == 1 || xuekernelPage.getStatus() == 0) {
            userStudentXueCount = coursewareStudentMapper.tongWarePin(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 2) {
            userStudentXueCount = coursewareStudentMapper.tongWarePin(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 3) {
            userStudentXueCount = coursewareStudentMapper.tongWarePin(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 4) {
            userStudentXueCount = coursewareStudentMapper.tongWarePin(xuekernelPage);
        }
        return kong(userStudentXueCount);
    }

    @Override
    public ResponseResult CourwareTongPinCi(XuekernelPage xuekernelPage) {
        if (xuekernelPage.getStatus()==null){
            xuekernelPage.setStatus(0);
        }
        ArrayList<SubjectTong> userStudentXueCount = null;
        if (xuekernelPage.getStatus() == 1 || xuekernelPage.getStatus() == 0) {
            userStudentXueCount = coursewareStudentMapper.tongWareShu(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 2) {
            userStudentXueCount = coursewareStudentMapper.tongWareShu(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 3) {
            userStudentXueCount = coursewareStudentMapper.tongWareShu(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 4) {
            userStudentXueCount = coursewareStudentMapper.tongWareShu(xuekernelPage);
        }
        return kong(userStudentXueCount);
    }

    @Override
    public ResponseResult CourwareTeaShu() {
        ArrayList<SubjectTong> userStudentXueCount = coursewareStudentMapper.tongWareTshu();
        return ResponseResult.okResult(userStudentXueCount);
    }


    @Override
    public ResponseResult BaoGao(SheBaoList sheBaos){
        sheBaos.setTime(new Date());
        CoursewareStudent coursewareStudent = new CoursewareStudent();
        ArrayList<SheBao> sheBaos1 = sheBaos.getSheBaos();
        coursewareStudent.setCoursewareId(sheBaos.getCid());
        coursewareStudent.setStudentId(sheBaos.getUid());
        coursewareStudent.setXueDate(sheBaos.getTime());
        if (sheBaos.getDu() == null) {
            sheBaos.setDu(0);
        }
        coursewareStudent.setDu(sheBaos.getDu());
        if (sheBaos.getShi() == null) {
            sheBaos.setShi(0);
        }
        coursewareStudent.setShi((double) sheBaos.getShi());
        //分数
        if (sheBaos.getZscore() == null) {
            sheBaos.setZscore(0);
        }
        coursewareStudent.setScore(sheBaos.getZscore());

        coursewareStudent.setPattern(sheBaos.getPattern());
        coursewareStudent.setDeid(1);
        if (null != sheBaos.getSheBaos() && sheBaos.getSheBaos().size() > 0) {
            coursewareStudent.setStatus(1);
        } else {
            coursewareStudent.setStatus(2);
        }
        coursewareStudentMapper.insert(coursewareStudent);
        CoursewareStudent coursewareStudent1 = coursewareStudentMapper.selectOne(Wrappers.<CoursewareStudent>lambdaQuery().eq(CoursewareStudent::getStudentId, coursewareStudent.getStudentId())
                .eq(CoursewareStudent::getCoursewareId, coursewareStudent.getCoursewareId())
                .eq(CoursewareStudent::getXueDate, coursewareStudent.getXueDate()));
        if (null != sheBaos.getSheBaos() && sheBaos.getSheBaos().size() > 0) {
            for (SheBao sheBao : sheBaos1) {
                CoursewareBaoStudent baoStudent = new CoursewareBaoStudent();
                baoStudent.setEsid(coursewareStudent1.getId());
                baoStudent.setBu(sheBao.getIndexStep());
                baoStudent.setName(sheBao.getStep());
                baoStudent.setRemark(sheBao.getRemark());
                baoStudent.setStatus(sheBao.getCompletionStatus());
                baoStudent.setShi(sheBao.getFinshTime());
                baoStudent.setScore(sheBao.getScore());
                coursewareBaoStudentMapper.insert(baoStudent);
            }
        }
        if (null != sheBaos.getSheBaos() && sheBaos.getSheBaos().size() > 0) {
            if (sheBaos.getPattern() == 2) {
                UserStudent userStudent = userStudentMapper.selectById(sheBaos.getUid());
                if (StringUtils.isNotBlank(userStudent.getOpenid())) {
                    accountService.SendWeChat(accountService.getToken(), userStudent.getOpenid(), sheBaos);
                }
            }
        }
        //报告
        return ResponseResult.errorResult(AppHttpCodeEnum.PQCG, AppHttpCodeEnum.PQCG.getErrorMessage());
    }


    @Override
    public ResponseResult StuTong() {
        List<Map<String, Object>> orders = coursewareStudentMapper.tongStu();
//补全数据库中不存在的日期，订单数为0
        List<String> dayList = dayList();
        boolean exists = false;
        for (int i = 0; i < dayList.size(); i++) {
            exists = false;
            for (Map<String, Object> hs : orders) {
                if (dayList.get(i).equals(hs.get("shoDate"))) {
                    exists = true;
                    break;
                }
            }
            if (!exists) {
                final String date2 = dayList.get(i);
                orders.add(i, new HashMap<String, Object>() {{
                    put("count", 0);
                    put("shoDate", date2);
                }});
            }
        }
        return ResponseResult.okResult(orders);
    }

    @Override
    public ResponseResult niuTong(XuekernelPage xuekernelPage) {
        if (xuekernelPage.getStatus() == null) {
            xuekernelPage.setStatus(0);
        }
        if (xuekernelPage.getStatus() == 1 || xuekernelPage.getStatus() == 0) {
            List<Map<String, Object>> orders = coursewareStudentMapper.tongSturevision(xuekernelPage);
//补全数据库中不存在的日期，订单数为0
            List<String> dayList = null;
            if (xuekernelPage.getStatus() == 1) {
                dayList = day(xuekernelPage.getTime(), 30);
            } else if (xuekernelPage.getStatus() == 0) {
                dayList = dayList();
            }
            boolean exists = false;
            for (int i = 0; i < dayList.size(); i++) {
                exists = false;
                for (Map<String, Object> hs : orders) {
                    if (dayList.get(i).equals(hs.get("shoDate"))) {
                        exists = true;
                        break;
                    }
                }
                if (!exists) {
                    final String date2 = dayList.get(i);
                    orders.add(i, new HashMap<String, Object>() {{
                        put("count", 0);
                        put("shoDate", date2);
                    }});
                }
            }
            return ResponseResult.okResult(orders);
        } else if (xuekernelPage.getStatus() == 2) {
            int j = differentDays(xuekernelPage.getMinTime(), xuekernelPage.getMaxTime());
            xuekernelPage.setFate(j);
            List<Map<String, Object>> orders = coursewareStudentMapper.tongSturevision(xuekernelPage);
            List<String> dayList = day(xuekernelPage.getMaxTime(), j);
            boolean exists = false;
            for (int i = 0; i < dayList.size(); i++) {
                exists = false;
                for (Map<String, Object> hs : orders) {
                    if (dayList.get(i).equals(hs.get("shoDate"))) {
                        exists = true;
                        break;
                    }
                }
                if (!exists) {
                    final String date2 = dayList.get(i);
                    orders.add(i, new HashMap<String, Object>() {{
                        put("count", 0);
                        put("shoDate", date2);
                    }});
                }
            }
            return ResponseResult.okResult(orders);
        } else if (xuekernelPage.getStatus() == 3) {
            List<Map<String, Object>> orders = coursewareStudentMapper.tongSturevision(xuekernelPage);
            int daysOfMonth = getDaysOfMonth(xuekernelPage.getTime());

            List<String> dayList = dayxue(xuekernelPage.getTime(), daysOfMonth);
            boolean exists = false;
            for (int i = 0; i < dayList.size(); i++) {
                exists = false;
                for (Map<String, Object> hs : orders) {
                    if (dayList.get(i).equals(hs.get("shoDate"))) {
                        exists = true;
                        break;
                    }
                }
                if (!exists) {
                    final String date2 = dayList.get(i);
                    orders.add(i, new HashMap<String, Object>() {{
                        put("count", 0);
                        put("shoDate", date2);
                    }});
                }
            }
            return ResponseResult.okResult(orders);
        } else if (xuekernelPage.getStatus() == 4) {
            List<Map<String, Object>> orders = coursewareStudentMapper.tongSturevision(xuekernelPage);
            int leqpYear = isLeqpYear(xuekernelPage.getTime());
            List<String> dayList = dayxue(xuekernelPage.getTime(), leqpYear);
            boolean exists = false;
            for (int i = 0; i < dayList.size(); i++) {
                exists = false;
                for (Map<String, Object> hs : orders) {
                    if (dayList.get(i).equals(hs.get("shoDate"))) {
                        exists = true;
                        break;
                    }
                }
                if (!exists) {
                    final String date2 = dayList.get(i);
                    orders.add(i, new HashMap<String, Object>() {{
                        put("count", 0);
                        put("shoDate", date2);
                    }});
                }
            }
            return ResponseResult.okResult(orders);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PQCG, AppHttpCodeEnum.PQCG.getErrorMessage());
    }

    @Override
    public ResponseResult proSho(XuekernelPage xuekernelPage) {
        if (xuekernelPage.getStatus() == null) {
            xuekernelPage.setStatus(0);
        }
        ArrayList<TSho> tSho = null;
        if (xuekernelPage.getStatus() == 1 || xuekernelPage.getStatus() == 0) {
            tSho = coursewareStudentMapper.proSho(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 2) {
            tSho = coursewareStudentMapper.proSho(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 3) {
            tSho = coursewareStudentMapper.proSho(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 4) {
            tSho = coursewareStudentMapper.proSho(xuekernelPage);
        }

        return ResponseResult.okResult(tSho);
    }

    @Override
    public ResponseResult ShoCourPro(XuekernelPage xuekernelPage) {
        if (xuekernelPage.getStatus() == null) {
            xuekernelPage.setStatus(0);
        }
        ArrayList<TSho> tShos = null;
        if (xuekernelPage.getStatus() == 1 || xuekernelPage.getStatus() == 0) {
            tShos = coursewareStudentMapper.proShoCour(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 2) {
            tShos = coursewareStudentMapper.proShoCour(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 3) {
            tShos = coursewareStudentMapper.proShoCour(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 4) {
            tShos = coursewareStudentMapper.proShoCour(xuekernelPage);
        }
        return ResponseResult.okResult(tShos);
    }

    @Override
    public ResponseResult ShoCourlou(XuekernelPage xuekernelPage) {
        if (xuekernelPage.getStatus() == null) {
            xuekernelPage.setStatus(0);
        }
        ArrayList<TSho> tShos = null;
        if (xuekernelPage.getStatus() == 1 || xuekernelPage.getStatus() == 0) {
            tShos = coursewareStudentMapper.proShoCourLou(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 2) {
            tShos = coursewareStudentMapper.proShoCourLou(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 3) {
            tShos = coursewareStudentMapper.proShoCourLou(xuekernelPage);
        } else if (xuekernelPage.getStatus() == 4) {
            tShos = coursewareStudentMapper.proShoCourLou(xuekernelPage);
        }
        return ResponseResult.okResult(tShos);
    }

    //计算一个年有多少天数
    public static int isLeqpYear(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY");
        String format = sdf.format(date);
        int i = Integer.parseInt(format);
        if (i % 4 == 0 && !(i % 100 == 0) || i % 400 == 0) {
            return 366;
        } else {
            return 365;
        }
    }

    //计算一个月有多少天数
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    @Override
    public ResponseResult CourwareTeaDu() {
        //总步数
        ArrayList<SubjectTong> userStudentXueCount = coursewareStudentMapper.tongWareDu();
        //完成的步数
        ArrayList<SubjectTong> user = coursewareStudentMapper.tongWareWanDu();
        //完成率
        ArrayList<SubjectTong> subjectTongs = new ArrayList<>();

        for (SubjectTong subjectTong : userStudentXueCount) {
            SubjectTong subjectTong1 = new SubjectTong();
            for (SubjectTong tong : user) {
                if (tong.getCount() != null || subjectTong.getCount() != null) {
                    if (tong.getSubject().equals(subjectTong.getSubject())) {
                        subjectTong1.setSubject(subjectTong.getSubject());
                        Integer du = tong.getCount() * 100 / subjectTong.getCount();
                        subjectTong1.setCount(du);
                        subjectTongs.add(subjectTong1);
                    }
                }
            }
        }
        return ResponseResult.okResult(subjectTongs);
    }

    @Override
    public ResponseResult xuethh() {

        XueTong teaShu = new XueTong();
        //总市长
        Integer time = coursewareStudentMapper.always();

        //平均市长
        Integer userCount = coursewareStudentMapper.alwaysAvg();

        //总授课时长
        XueTong xueTong = coursewareStudentMapper.TearShi();
        if (xueTong == null) {
            teaShu.setShoshi(0);
            teaShu.setPinsho(0.0);
        }
        if (xueTong != null) {
            teaShu.setShoshi(xueTong.getShoshi());
            teaShu.setPinsho(xueTong.getPinsho());
        }
        teaShu.setZong(time);
        teaShu.setPinshi((double) userCount);
        //累计节省耗材（g）
        return ResponseResult.okResult(teaShu);
    }


}
